#!usr/bin/env python
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tap_connector19_hbase.py
@version:
@time: 2022/01/13 
"""
import happybase
from TapClientConnector.connector.tap_connector00_base import *


class TAPHbaseConnector(TAPConnectorBase):

    def __init__(self, kwargs):
        super().__init__(kwargs)
        pop_user = self.FORMAT_DB_INFO.pop('user')
        pop_database_name = self.FORMAT_DB_INFO.pop('database')
        pop_password = self.FORMAT_DB_INFO.pop('password')
        self.FORMAT_DB_INFO['table_prefix'] = pop_database_name
        print(self.FORMAT_DB_INFO)
        self.connector = happybase.Connection(**self.FORMAT_DB_INFO, autoconnect=False)
        self.connector.open()

    def __hbase_find(self, table):
        tmp = []
        for one in self.connector.table(table).scan():
            tmp.append(one)
        return tmp

    def query(self, sql):
        result = self._query(sql)
        self.connector.close()
        return result

    def _query(self, sql):
        if isinstance(sql, tuple):
            __op = sql[0]
            __data = sql[1]
            if __op == "find":
                return self.__hbase_find(*__data)
        else:
            raise CustomHbaseSyntaxError

    @generate_logger
    def generate_select(self, table=None, column='*', where=None, order=''):
        if where is None:
            where = {}
        if table:
            self.CURRENT_TABLE = table
        if column == '*':
            column = {"_id": 0}
        if column == 'count(*)':
            return "count", (self.CURRENT_TABLE, where)
        return "find", (self.CURRENT_TABLE,)
